package com.example.user.coolrun.runnershign;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.user.coolrun.R;
import com.example.user.coolrun.highscore.HighscoreAdapter;

public class HighScoreActivity extends Activity {

    private HighscoreAdapter highScoreAdapter = null;
    private TableLayout highscoreTable;

    // ---------------------------------------------------
    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_high_score);
        highScoreAdapter = new HighscoreAdapter(this);
        highScoreAdapter.open();

        highscoreTable = (TableLayout) findViewById(R.id.highscoreTable);

        final Context context = this;
        final Handler handler = new Handler();

        Toast.makeText(context,"欢迎查看排行榜！" , Toast.LENGTH_SHORT).show();
        handler.postDelayed(new Runnable() {

            public void run() {
                showLocalScore();
            }
        }, 500);
    }

    private void showLocalScore() {
        highscoreTable.removeAllViews();

        Cursor c = highScoreAdapter.fetchScores("0");
        if (c.isAfterLast()) {
            Toast.makeText(this, "快去创造自己的记录吧！", Toast.LENGTH_SHORT).show();
            return;
        }
        int currentPlace = 1;
        do {
            final String placeString = ""+(currentPlace++)+".";
            final String scoreString = c.getString(2);
            final String nameString = c.getString(1);

            View additional;

            if (c.getString(3).equalsIgnoreCase("0")) {
                additional = new Button(this);

                final Context context = this;
                final int id = c.getInt(0);

                additional.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        AlertDialog.Builder alert = new AlertDialog.Builder(context);
                        alert.setTitle("确定要删除这条记录吗 ?");
                        alert.setMessage("姓名: " + nameString + "\n分数: " + scoreString);
                        //点击确定
                        alert.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                highScoreAdapter.delete(id);
//
                                highScoreAdapter.toastMessage("删除成功");
                                runOnUiThread(new Runnable() {
                                    public void run() {
                                        showLocalScore();

                                    }
                                });
                            }

                        });
                        //点击取消
                        alert.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                            }
                        });
                        alert.show();
                    }
                });
                additional.setBackgroundResource(R.drawable.highscore_submit);

                TableRow.LayoutParams paramsOfSubmitButton = new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT, 3.0f);
                additional.setLayoutParams(paramsOfSubmitButton);
            } else {
                additional = new TextView(this, null, android.R.attr.textAppearanceSmallInverse);

                TableRow.LayoutParams paramsOfAdditional = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 3.0f);
                paramsOfAdditional.gravity = Gravity.CENTER_VERTICAL | Gravity.RIGHT;
                additional.setLayoutParams(paramsOfAdditional);
            }

            generateLine(placeString, scoreString, nameString, additional);

        }
        while(c.moveToNext());

    }


    private void generateLine(String placeString, String scoreString, String nameString, View additional) {

        TextView place = new TextView(this, null, android.R.attr.textAppearanceLargeInverse);
        place.setText(placeString);
        TableRow.LayoutParams paramsOfPlace = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 2.0f);
        paramsOfPlace.gravity = Gravity.CENTER_VERTICAL | Gravity.RIGHT;
        place.setLayoutParams(paramsOfPlace);

        TextView score = new TextView(this, null, android.R.attr.textAppearanceMediumInverse);
        score.setText(scoreString);
        TableRow.LayoutParams paramsOfScore = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 3.0f);
        paramsOfScore.gravity = Gravity.CENTER;
        score.setLayoutParams(paramsOfScore);

        TextView name = new TextView(this, null, android.R.attr.textAppearanceMediumInverse);
        name.setText(nameString);
        TableRow.LayoutParams paramsOfName = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 10.0f);
        paramsOfName.gravity = Gravity.CENTER_VERTICAL | Gravity.LEFT;
        name.setLayoutParams(paramsOfName);

        addLine(place, score, name, additional);
    }

    private void addLine(View place, View score, View name, View additional) {
        TableRow tr = new TableRow(this);

        tr.setLayoutParams(new TableRow.LayoutParams(
                TableRow.LayoutParams.FILL_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT));

        tr.addView(place);
        tr.addView(score);
        tr.addView(name);
        tr.addView(additional);

        highscoreTable.addView(tr);
        ImageView line = new ImageView(this);
        line.setBackgroundResource(R.drawable.highscore_line);
        highscoreTable.addView(line);
    }
    // ---------------------------------------------------------
    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (highScoreAdapter != null) {
            highScoreAdapter.close();
        }
    }
}
